/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import services.ConvIntByte;

/**
 *
 * @author minaev
 */
public class DataExtract {

    public static int getSensor(byte[] buf, int len, int nsensor) {
        if (nsensor < Configurator.COUNT_SENSOR) {
            if (len >= (1 + nsensor * 2)) {
                return (ConvIntByte.byte2Int(buf[nsensor * 2 + 1]) << 8)
                        | (ConvIntByte.byte2Int(buf[nsensor * 2 + 2]));
            } else {
                StendLogger.log(StendLogger.MSG_WARNING, "Small buffer for get "
                        + "sensor. Length buf " + len
                        + " min need length: " + (1 + nsensor * 2)
                        + " N sensor: " + nsensor);
                return -1;
            }
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Requested bad number "
                    + "sensor: " + nsensor
                    + " max sensor: " + (Configurator.COUNT_SENSOR - 1));
            return -1;
        }
    }

    public static long getPosition(byte[] buf, int len) {
        if (len >= 5) {
            return (ConvIntByte.byte2Int(buf[len - 4]) << 24)
                    | (ConvIntByte.byte2Int(buf[len - 3]) << 16)
                    | (ConvIntByte.byte2Int(buf[len - 2]) << 8)
                    | (ConvIntByte.byte2Int(buf[len - 1]));
        } else {
            StendLogger.log(StendLogger.MSG_WARNING, "Small buffer for get "
                    + "position. Length buf " + len
                    + " min need length: 5");
            return -1;
        }
    }

    public static int getSensor(byte[] buf, int nsensor) {
        return getSensor(buf, buf.length, nsensor);
    }

    public static long getPosition(byte[] buf) {
        return getPosition(buf, buf.length);
    }

    /**
     * Генерация 4-байтового параметра из массива
     *
     * @param buf - массив
     * @param len - с какого элемента считать 4 байта
     * @return
     */
    public static long getLongParam(byte[] buf, int len) {
        return (ConvIntByte.byte2Int(buf[len]) << 24) | (ConvIntByte.byte2Int(buf[len+1])  << 16)
                | (ConvIntByte.byte2Int(buf[len+2])  << 8) | (ConvIntByte.byte2Int(buf[len+3]) );
    }
}
